//
//  JXCustomAlert.swift
//  GodoxFlash
//
//  Created by godox on 2021/11/30.
//

import UIKit

open class JXCustomAlert: UIView {


    public enum AlertType {
        case alert
        case sheet
        case editText
    }
    
    public enum NeedDisTap {
        case need
        case no
    }
    
    public var needDisTapType: NeedDisTap = .need
    
    public var type: AlertType = .alert
    
    public var maskBgView: UIView?
    private var missTapGesture: UIGestureRecognizer?
    
    
    
    public init() {
        super.init(frame: .zero)
        
        maskBgView = UIView()
        maskBgView?.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        
        missTapGesture = UITapGestureRecognizer(target: self, action: #selector(missTap))
        missTapGesture?.delegate = self
        
        
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    open func show() {

        JXUITools.getWindows!.addSubview(maskBgView!)
        maskBgView!.translatesAutoresizingMaskIntoConstraints = false
        let topConstraint = NSLayoutConstraint(item: maskBgView!, attribute: .top, relatedBy: .equal, toItem: JXUITools.getWindows!, attribute: .top, multiplier: 1.0, constant: 0)
        let leftConstraint = NSLayoutConstraint(item: maskBgView!, attribute: .left, relatedBy: .equal, toItem: JXUITools.getWindows!, attribute: .left, multiplier: 1.0, constant: 0)
        let rightConstraint = NSLayoutConstraint(item: maskBgView!, attribute: .right, relatedBy: .equal, toItem: JXUITools.getWindows!, attribute: .right, multiplier: 1.0, constant: 0)
        let bottomConstraint = NSLayoutConstraint(item: maskBgView!, attribute: .bottom, relatedBy: .equal, toItem: JXUITools.getWindows!, attribute: .bottom, multiplier: 1.0, constant: 0)
        JXUITools.getWindows!.addConstraints([topConstraint,leftConstraint,rightConstraint,bottomConstraint])
        maskBgView?.center = JXUITools.getWindows!.center
        
        maskBgView?.addSubview(self)
        setCenterShow()
        showAnimation()
    }
    
    public func setCenterShow() {
        
        self.center = maskBgView!.center
        
    }
    
    open func dissMiss() {
        
        hideAnimation()
    }
    
    private func showAnimation() {
        
        if needDisTapType == .need {
            maskBgView?.addGestureRecognizer(missTapGesture!)
        }
        
        if type == .alert {
            self.center = maskBgView!.center
            maskBgView?.layoutIfNeeded()
            
            alpha = 0.0
            
            UIView.animate(withDuration: 0.35) { [self] in
                self.alpha = 1.0
                self.center = maskBgView!.center
            }
        } else if type == .editText {
            
            self.center.x = maskBgView!.center.x
            self.center.y = maskBgView!.center.y - 50.0
            maskBgView?.layoutIfNeeded()
            
            alpha = 0.0
            
            UIView.animate(withDuration: 0.35) { [self] in
                self.alpha = 1.0
                self.center.x = maskBgView!.center.x
                self.center.y = maskBgView!.center.y - 50.0
            }
            
        } else {
            maskBgView?.layoutIfNeeded()
            self.frame.origin.y = maskBgView!.frame.origin.y + maskBgView!.frame.size.height
            UIView.animate(withDuration: 0.35) {
                self.frame.origin.y = JXUITools.screenHeight - self.frame.size.height
            }
        }
    }
    
    private func hideAnimation() {
        
        if type == .alert || type == .editText{
            
            UIView.animate(withDuration: 0.35) {
                self.alpha = 0.0
                self.maskBgView?.alpha = 0.0
            } completion: { finished in
                self.removeFromSuperview()
                self.maskBgView?.removeFromSuperview()
            }

        } else {
            UIView.animate(withDuration: 0.35) {
                self.frame.origin.y = self.maskBgView!.frame.origin.y + self.maskBgView!.frame.size.height
                self.maskBgView?.alpha = 0.0
            } completion: { finished in
                self.alpha = 0.0
                self.removeFromSuperview()
                self.maskBgView?.removeFromSuperview()
            }
            
        }
        
    }
    
    
    @objc private func missTap() {
        
        dissMiss()
    }
    
}

extension JXCustomAlert: UIGestureRecognizerDelegate{
    
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if NSStringFromClass((touch.view?.classForCoder)!) == "UITableViewCellContentView" {
            return false
        }
        return true
    }
}
